package com.znf4.controller.malls;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.znf4.ad.entity.Ad;
import com.znf4.ad.server.AdServer;
import com.znf4.entity.ApiCommonResultVo;
import com.znf4.good.crops.entity.Crops;
import com.znf4.good.crops.server.CropsServer;
import com.znf4.good.imgUrl.entity.ImgUrl;
import com.znf4.good.imgUrl.server.ImgUrlServer;
import com.znf4.good.specs.entity.Specs;
import com.znf4.good.specs.server.SpecsServer;
import com.znf4.notice.entity.Notice;
import com.znf4.notice.server.NoticeServer;

/**
 * 商城商品模块
 */
@RestController
@RequestMapping("/gzh/crops")

public class CropsGoodController {

	private static final Logger LOG = LoggerFactory.getLogger(CropsGoodController.class);

	@Autowired
	private AdServer adServer;

	@Autowired
	private NoticeServer noticeServer;

	@Autowired
	private CropsServer cropsServer;

	@Autowired
	private SpecsServer specsServer;

	@Autowired
	private ImgUrlServer imgUrlServer;
	
	

	/**
	 * 函数功能说明 ： 获取所有商城商品
	 *
	 * @参数： @return @return String @throws
	 */
	@RequestMapping(value = "/index", method = { RequestMethod.POST, RequestMethod.GET })
	public ApiCommonResultVo index(Map<String, Object> resultMap) {

		try {
			List<Ad> ads = adServer.queryAll(new Ad());
			LOG.info("广告列表 ：" + ads.toString());

			Notice notice = new Notice();
			notice.setType("公告");
			List<Notice> notices = noticeServer.queryAll(notice);
			LOG.info("公告列表 ：" + notices.toString());

			List<Crops> crops = cropsServer.queryAll(new Crops());
			LOG.info("商品列表 ：" + crops.toString());

			resultMap.put("ads", ads);
			resultMap.put("notices", notices);
			resultMap.put("crops", crops);

			return new ApiCommonResultVo(0, "", resultMap);

		} catch (Exception e) {
			LOG.info("获取所有商城商品接口异常 ：" + e.getMessage());
			return new ApiCommonResultVo(-2, "系统异常", "");
		}

	}

	/**
	 * 函数功能说明 ： 获取农作物商品莫个商品的详情
	 *
	 * @参数： @return @return String @throws
	 */
	@RequestMapping(value = "/goodInfo", method = { RequestMethod.POST, RequestMethod.GET })
	public ApiCommonResultVo index(Crops crops , Map<String, Object> resultMap ) {

		try {
			crops = cropsServer.queryOne(crops);
			LOG.info("商品基本信息 ：" + crops.toString());

			Specs specs = new Specs();
			specs.setGood_id(crops.getId());
			List<Specs> specssList = specsServer.queryAll(specs);

			ImgUrl imgUrl = new ImgUrl();
			imgUrl.setGood_id(crops.getId());
			List<ImgUrl> imgUrls = imgUrlServer.queryAll(imgUrl);

			resultMap.put("crops", crops);
			resultMap.put("specs", specssList);
			resultMap.put("imgUrls", imgUrls);

			return new ApiCommonResultVo(0, "", resultMap);

		} catch (Exception e) {
			LOG.info("获取农作物商品莫个商品的详情异常 ：" + e.getMessage());
			return new ApiCommonResultVo(-2, "系统异常", "");
		}

	}

}
